<?php
namespace app\superadmin\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class ChangeImageDomain extends Command
{
  protected function configure()
  {
      $this->setName('image:domain')->setDescription('修改图片域名');
  }

  protected function execute(Input $input, Output $output)
  {
    
    $output->writeln("开始修改images表");
    Db::name('images')->chunk(10, function($imgs) {
      foreach($imgs as $img){
        $change = [];
        $banner = json_decode($img['banner'], true);
        if(is_array($banner)){
          foreach($banner as $k=>$v){
            $banner[$k] = $this->changeDomain($v);
          }
          $change['banner'] = json_encode($banner);
        }
        $fields = ['fwys','jcyq','jcxm','fwlc','hftp','header','footer','chapter'];
        foreach($fields as $field){
          if($img[$field]){
            $change[$field] = $this->changeDomain($img[$field]);
          }
        }      
        Db::name('images')->where('id', $img['id'])->update($change);
      }
    });
    $output->writeln("修改images表完成");

    // 
    $output->writeln("修改report表开始");
    Db::name('report')->chunk(100, function($reports){
      foreach($reports as $report){
        $change = [];
        $banner = json_decode($report['banner'], true);
        if(is_array($banner) && $banner){
          foreach($banner as $k=>$b){
            if($b){
              $b = $this->changeDomain($b);
            }
            $banner[$k] = $b; 
          }
          $change['banner'] = json_encode($banner);
        }
        
        $images = json_decode($report['images'], true);
        if(is_array($images) && $images){
          foreach($images as $k=>$img){
            if($img){
              $img = $this->changeDomain($img);
            }
            $images[$k] = $img; 
          }
          $change['images'] = json_encode($images);
        }
        if(empty($change)) continue;
        Db::name('report')->where('id', $report['id'])->update($change);
      }
    });
    $output->writeln("修改report表完成");

    // 
    $output->writeln("修改report_list表开始");
    Db::name('report_list')->chunk(100, function($lists){
      foreach($lists as $list){
        $imgs = json_decode($list['imgs'], true);
        if(is_array($imgs) && $imgs){
          foreach($imgs as $k=>$v){
            $imgs[$k] = $this->changeDomain($v);
          }
          $change = json_encode($imgs);
          if($change == $list['imgs']) continue;
          Db::name('report_list')->where('id', $list['id'])->update(['imgs'=>$change]);
        }
      }
    });

    $output->writeln("修改report_list表完成");
    // 
    $output->writeln("修改service表开始");
    Db::name('service')->chunk(100, function($services){
      foreach($services as $service){
       if($service['img']){
        $img = $this->changeDomain($service['img']);
        if($service['img'] == $img) continue;
        Db::name('service')->where('id', $service['id'])->update(['img'=>$img]);
       }
      }
    });
    $output->writeln("修改service表完成");
    // 
    $output->writeln("修改user表开始");
    Db::name('user')->chunk(100, function($users){
      foreach($users as $user){
        if($user['zhang']){
          $zhang = $this->changeDomain($user['zhang']);
          if($zhang == $user['zhang']) continue;
          Db::name('user')->where('id', $user['id'])->update(['zhang'=>$zhang]);
        }
      }
    });
    $output->writeln("修改user表完成");
  }

  protected function changeDomain(string $url){
    $find_domain = '/^https?:\/\/zjjtoss.xsh618.com/';
    $domain = "http://zjjtoss.ccicgdauto.com";
    return preg_replace($find_domain, $domain, $url);
  }

}